package com.water.wfo.user.business.service.impl;

import com.water.wfo.component.util.StringUtil;
import com.water.wfo.sys.pojo.dto.MenuDto;
import com.water.wfo.sys.pojo.entity.MenuEntity;
import com.water.wfo.sys.pojo.request.RoleRequest;
import com.water.wfo.sys.pojo.response.MenuResponse;
import com.water.wfo.sys.pojo.response.RoleResponse;
import com.water.wfo.user.ability.MenuAbility;
import com.water.wfo.user.business.service.IMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * packageName com.water.wfo.user.business.service.impl
 *
 * @author
 * @className MenuServiceImpl(处以class为网)
 * @date 2024/9/9
 * @description T0D0
 */
@Component
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuAbility menuAbility;

    @Override
    public MenuResponse queryMenuTreeInfo(RoleRequest request) {
        MenuResponse response = new MenuResponse();
        List<MenuEntity> menuEntities= menuAbility.queryAllMenuEntity();
        if(CollectionUtils.isNotEmpty(menuEntities)){
            List<MenuEntity> parentMenus = menuEntities.stream().filter(s -> s.getParentId() == 1).collect(Collectors.toList());
            parentMenus.stream().forEach(menu ->{
                if(menu.getName().contains(request.getName())){
                    menu.setChecked(1);
                    return;
                }
            });
            List<MenuEntity> childMenus = menuEntities.stream().filter(s -> s.getParentId() > 1).collect(Collectors.toList());
            childMenus.stream().forEach(menu->{
                if(StringUtil.isNotBlank(request.getName()) && menu.getName().contains(request.getName())){
                    menu.setChecked(1);
                    return;
                }
            });
            response.setParenMenus(parentMenus);
            response.setChildMenus(childMenus);
        }
        return response;
    }

}
